
package com.cortex.pineapple.adapters;

import java.util.ArrayList;
import java.util.List;

import com.cortex.pineapple.R;
import com.cortex.pineapple.view.CortexTextView;
import com.cortext.pineapple.link.ImageLink;
import com.cortext.pineapple.link.Link;
import com.cortext.pineapple.link.NewsLink;
import com.cortext.pineapple.link.VideoLink;
import com.cortext.pineapple.link.WebLink;
import com.cortext.pineapple.link.Link.OnDataChangedListener;
import com.squareup.picasso.Picasso;

import android.app.Activity;
import android.content.Context;
import android.provider.Contacts.Intents.UI;
import android.test.UiThreadTest;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class NewsAdapter extends LinkAdapter<NewsLink> {

	public NewsAdapter(Context context) {
		super(context);
		links = new ArrayList<NewsLink>();
	}
	public NewsAdapter(Context context, List<NewsLink> links) {
		super(context,links);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		NewsLink link = links.get(position);
		ViewGroup linkView = (ViewGroup) convertView; 
		if(linkView == null){
			LayoutInflater inflater = LayoutInflater.from(context);
			linkView = (ViewGroup) inflater.inflate(R.layout.view_news_link, null);

			final ViewGroup finalLinkView = linkView;
			link.setOnDataChangedListener(new OnDataChangedListener() {

				@Override
				public void dataChanged(Link link) {
					buildItemView((NewsLink) link, finalLinkView);
				}
			});
		}
		buildItemView(link, linkView);

		return linkView;
	}
	@Override
	protected void buildItemView(final NewsLink link, final ViewGroup linkView) {

		((Activity)context).runOnUiThread(new Runnable() {

			@Override
			public void run() {
				// add under line 
				CortexTextView titleText = (CortexTextView) linkView.findViewById(R.id.title);
				ImageView icon = (ImageView)linkView.findViewById(R.id.icon);
				ImageView image = (ImageView)linkView.findViewById(R.id.image);

				titleText.setText(link.getTitle());
				((TextView)linkView.findViewById(R.id.content)).setText(link.getDescription());
				((TextView)linkView.findViewById(R.id.url)).setText(link.getUrl());
				if(link.getIcon() != null){
					icon.setVisibility(View.VISIBLE);
					icon.setImageBitmap(link.getIcon());
				}
				Picasso.with(context)
				.load(link.getImageUrl())
				.resizeDimen(R.dimen.imageCellWidth, R.dimen.imageCellHeight)
				.noFade()
				.into(((ImageView)linkView.findViewById(R.id.image)));
				if(link.getImageUrl() == null){
					image.setVisibility(View.GONE);
				}
				else{
					image.setVisibility(View.VISIBLE);
				}
			}
		});
	}
}
